﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using SGCG.Model;
using SGCG.Helpers;

namespace SGCG.Actions
{
    public class CreateRegularPolygonAction : Action
    {
        private readonly BorderOptions pallete = new BorderOptions();

        public CreateRegularPolygonAction(PellBitmap pellBitmap) : base(pellBitmap) { }

        public override void HandleClick(MouseButtonEventArgs eventArgs, UIElement plotArea)
        {
            Point clickLocation = eventArgs.GetPosition(plotArea);

            int x = (int)(clickLocation.X + 0.5);
            int y = (int)(clickLocation.Y + 0.5);

            if (RegularPolygon == null)
            {
                NumberOfSidesAskWindow nosaw = new NumberOfSidesAskWindow();
                nosaw.Show();
                nosaw.Closed += (s, e) =>
                    {
                        int nsides = nosaw.NumberOfSides;
                        UserMessage.Debug("nsides: " + nsides.ToString());
                        if (nsides < 3)
                            nsides = 3;
                        using (new BatchRedrawOperation(PellBitmap))
                        {
                            this.PellBitmap.AddObject(RegularPolygon = new RegularPolygon(null, nsides)
                            {
                                X = x,
                                Y = y,
                                Radius = 0,
                                AngleInit = 0
                            });
                        }
                    };
            }
            else
            {
                RegularPolygon = null;
            }
        }

        public override void HandleMouseMove(MouseEventArgs eventArgs, UIElement plotArea)
        {
            if (RegularPolygon != null)
            {
                using (new BatchRedrawOperation(PellBitmap))
                {
                    Point mouseLocation = eventArgs.GetPosition(plotArea);
                    Point p = new Point(RegularPolygon.X, RegularPolygon.Y);
                    RegularPolygon.Radius = (int)Math.Round(p.Subtract(mouseLocation).Norm());
                    p = p.Subtract(mouseLocation);
                    RegularPolygon.AngleInit = Math.Atan2(p.Y, p.X);
                }
            }
        }

        private RegularPolygon regularPolygon;
        protected RegularPolygon RegularPolygon
        {
            get { return regularPolygon; }
            set
            {
                regularPolygon = value;
                pallete.IHasBorder = value;
            }
        }

        public override UIElement Pallete
        {
            get
            {
                return this.pallete;
            }
        }
    }
}
